package top.hcode.hoj.service.file;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import lombok.SneakyThrows;
import org.nutz.lang.Files;
import org.nutz.lang.Times;
import org.springframework.beans.factory.annotation.Autowired;
import top.hcode.hoj.common.exception.StatusForbiddenException;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import top.hcode.hoj.common.exception.StatusFailException;
import top.hcode.hoj.common.exception.StatusSystemErrorException;
import top.hcode.hoj.common.result.CommonResult;
import top.hcode.hoj.common.result.ResultStatus;
import top.hcode.hoj.manager.file.ImageManager;
import top.hcode.hoj.mapper.FileMapper;
import top.hcode.hoj.pojo.entity.common.File;
import top.hcode.hoj.pojo.entity.group.Group;
import top.hcode.hoj.utils.AliyunOSSUtils;
import top.hcode.hoj.utils.ShiroUtils;

import javax.annotation.Resource;
import java.io.InputStream;
import java.util.Map;

/**
 * @Author: Himit_ZH
 * @Date: 2022/3/10 15:41
 * @Description:
 */
@Service
public class ImageService {

    @Resource
    private ImageManager imageManager;
    @Autowired
    private FileMapper fileMapper;


    public CommonResult<Map<Object, Object>> uploadAvatar(MultipartFile image) {
        try {
            return CommonResult.successResponse(imageManager.uploadAvatar(image));
        } catch (StatusFailException e) {
            return CommonResult.errorResponse(e.getMessage());
        } catch (StatusSystemErrorException e) {
            return CommonResult.errorResponse(e.getMessage(), ResultStatus.SYSTEM_ERROR);
        }
    }


    public CommonResult<Group> uploadGroupAvatar(MultipartFile image, Long gid) {
        try {
            return CommonResult.successResponse(imageManager.uploadGroupAvatar(image, gid));
        } catch (StatusFailException e) {
            return CommonResult.errorResponse(e.getMessage());
        } catch (StatusSystemErrorException e) {
            return CommonResult.errorResponse(e.getMessage(), ResultStatus.SYSTEM_ERROR);
        } catch (StatusForbiddenException e) {
            return CommonResult.errorResponse(e.getMessage(), ResultStatus.FORBIDDEN);
        }
    }


    public CommonResult<Map<Object, Object>> uploadCarouselImg(MultipartFile image) {
        try {
            return CommonResult.successResponse(imageManager.uploadCarouselImg(image));
        } catch (StatusFailException e) {
            return CommonResult.errorResponse(e.getMessage());
        } catch (StatusSystemErrorException e) {
            return CommonResult.errorResponse(e.getMessage(), ResultStatus.SYSTEM_ERROR);
        }
    }

    @SneakyThrows
    public CommonResult<String> uploadCourseImg(MultipartFile image) {
        File file = uploadFile(image, "course_img");
        if (file != null) {
            return CommonResult.successResponse(file.getFilePath(), "success");
        } else {
            return CommonResult.errorResponse("上传失败", ResultStatus.SYSTEM_ERROR);
        }
    }

    public CommonResult<String> uploadMdImg(MultipartFile image) {
        File file = uploadFile(image, "md_img");
        if (file != null) {
            return CommonResult.successResponse(file.getFilePath(), "success");
        } else {
            return CommonResult.errorResponse("上传失败", ResultStatus.SYSTEM_ERROR);
        }
    }

    @SneakyThrows
    public File uploadFile(MultipartFile multipartFile, String type) {
        String originalFilename = multipartFile.getOriginalFilename();
        String suffix = Files.getSuffix(originalFilename);
        InputStream inputStream = multipartFile.getInputStream();

        String key = Times.format("yyyyMMdd", Times.now()) + "/" + IdUtil.getSnowflakeNextId() + suffix;
        String url = AliyunOSSUtils.uploadFile(inputStream, key);

        File file = new File();
        file.setUid(ShiroUtils.getUid());
        file.setGid(null);
        file.setType("course_img");
        file.setName(StrUtil.subAfter(key, "/", true));
        file.setSuffix(StrUtil.removePrefix(suffix, "."));
        file.setFolderPath(StrUtil.subBefore(key, "/", true));
        file.setFileSize(multipartFile.getSize());
        file.setFilePath(url);
        file.setDelete(false);
        fileMapper.insert(file);

        return file;
    }
}
